from flask.views import MethodView

import json


class BaseMethodView(MethodView):
    MSGS = {'200': '请求成功!', '404': '请求失败!'}

    def __init__(self):
        self.data = dict(
            code=200,
            msg=BaseMethodView.MSGS['200'],
            data=[]
        )

    def result(self):
        return json.dumps(self.data)

    def set_data(self, data):
        self.data['data'].append(data)

    def set_msg(self, code):
        self.data['code'] = code
        self.data['msg'] = BaseMethodView.MSGS[
            '{}'.format(code)
        ]
